#!/usr/bin/env python3

import json
from pathlib import Path
from typing import Iterable

SRC = Path("src")
STAGE3 = Path("build/release/stage3")
STAGE3_TEMP = STAGE3 / "lib" / "temp"
STAGE3_LEAN = STAGE3 / "lib" / "lean"


def print_result(metric: str, value: float, unit: str | None = None) -> None:
    data = {"metric": metric, "value": value}
    if unit is not None:
        data["unit"] = unit
    print(f"radar::measurement={json.dumps(data)}")


def measure_bytes(topic: str, paths: Iterable[Path], files: bool = True) -> None:
    amount = 0
    total = 0
    for path in paths:
        amount += 1
        total += path.stat().st_size
    if files:
        print_result(f"{topic}//files", amount)
    print_result(f"{topic}//bytes", total, "B")


def measure_lines(topic: str, paths: Iterable[Path]) -> None:
    amount = 0
    total = 0
    for path in paths:
        amount += 1
        with open(path) as f:
            total += sum(1 for _ in f)
    print_result(f"{topic}//files", amount)
    print_result(f"{topic}//lines", total)


if __name__ == "__main__":
    measure_bytes(
        "size/libleanshared.so",
        [STAGE3_LEAN / "libleanshared.so"],
        files=False,
    )

    # Stdlib
    measure_lines("size/all/.c", STAGE3_TEMP.glob("**/*.c"))
    measure_bytes("size/all/.ir", STAGE3_LEAN.glob("**/*.ir"))
    measure_lines("size/all/.cpp", SRC.glob("**/*.cpp"))
    measure_lines("size/all/.lean", SRC.glob("**/*.lean"))
    measure_bytes("size/all/.ilean", STAGE3_LEAN.glob("**/*.ilean"))
    measure_bytes("size/all/.olean", STAGE3_LEAN.glob("**/*.olean"))
    measure_bytes("size/all/.olean.server", STAGE3_LEAN.glob("**/*.olean.server"))
    measure_bytes("size/all/.olean.private", STAGE3_LEAN.glob("**/*.olean.private"))

    # Init
    measure_bytes("size/init/.olean", STAGE3_LEAN.glob("Init/**/*.olean"))
    measure_bytes("size/init/.olean.server", STAGE3_LEAN.glob("Init/**/*.olean.server"))
    measure_bytes(
        "size/init/.olean.private", STAGE3_LEAN.glob("Init/**/*.olean.private")
    )
